package com.ruoyi.controller.system;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.vo.AjaxResult;
import com.ruoyi.controller.BaseController;
import com.ruoyi.domain.SysPost;
import com.ruoyi.service.SysPostService;

@RestController
@RequestMapping("system/post")
public class SysPostController extends BaseController{
	
	@Autowired
	private SysPostService sysPostService;
	
	/**
	 * 获取岗位列表
	 * @param post
	 * @return
	 */
	@GetMapping("list")
	public TableDataInfo list(SysPost post) {
		logger.info("获取岗位列表,post:{}",post);
		startPage();
		List<SysPost> list = sysPostService.selectPostList(post);
		return getDataTable(list);
	}
	
	@GetMapping(value = "/{postId}")
	public AjaxResult getInfo(@PathVariable Long postId) {
		return AjaxResult.success(sysPostService.selectPostById(postId));
	}
	
	/**
	 * 新增岗位
	 * @param post
	 * @return
	 */
	@PostMapping
	public AjaxResult add(@Validated @RequestBody SysPost post) {
		logger.info("新增岗位,post:{}",post);
		if(!sysPostService.checkPostNameUnique(post)) {
			return error("新增岗位" + post.getPostName() + "失败，岗位名称已存在");
		} else if (!sysPostService.checkPostCodeUnique(post)) {
			return error("新增岗位" + post.getPostCode() + "失败，岗位编码已存在");
		}
		post.setCreateBy(getUsername());
		return toAjax(sysPostService.insertPost(post));
	}
	
	/**
	 * 修改岗位
	 * @param post
	 * @return
	 */
	@PutMapping
	public AjaxResult edit(@Validated @RequestBody SysPost post) {
		logger.info("修改岗位,post:{}",post);
		if(!sysPostService.checkPostNameUnique(post)) {
			return error("修改岗位" + post.getPostName() + "失败，岗位名称已存在");
		} else if (!sysPostService.checkPostCodeUnique(post)) {
			return error("修改岗位" + post.getPostCode() + "失败，岗位编码已存在");
		}
		post.setUpdateBy(getUsername());
		return toAjax(sysPostService.updatePost(post));
	}
	
	/**
	 * 删除岗位
	 * @param postIds
	 * @return
	 */
	@DeleteMapping(value = "/{postId}")
	public AjaxResult remove(@PathVariable Long postId) {
		logger.info("删除岗位,postId:{}",postId);
		return toAjax(sysPostService.deletePostById(postId));
	}
	
}
